﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KCMS.BusinessLogic.Interfaces;
using KCMS.BusinessLogic.Models;
using KCMS.DataAcsess.Interfaces;
using StructureMap;
using AutoMapper;

namespace KCMS.BusinessLogic.Engine
{
    public class ProductService : IProductService
    {
        private IProductManager pgm = ObjectFactory.GetInstance<IProductManager>();
        private IProductCategoryService productCatService = ObjectFactory.GetInstance<IProductCategoryService>();
        public Models.Product getById(Guid id)
        {
            DataAcsess.Entitys.ProductEntity productEntity = pgm.getById(id);
            return Mapper.Map<DataAcsess.Entitys.ProductEntity, Models.Product>(productEntity);
        }
        public Models.Product getByCodeIndex(String code, int index)
        {


            var productEntity = (from t in pgm.getAll()
                                 where (t.Code.Equals(code) && t.Idx.Equals(index))
                                 select t).SingleOrDefault();
            return Mapper.Map<DataAcsess.Entitys.ProductEntity, Models.Product>(productEntity);
        }

        public IEnumerable<Models.Product> getAll()
        {
            var products = (from t in pgm.getAll()
                            orderby t.Article
                            select t).ToList();
            var model = Mapper.Map<IEnumerable<DataAcsess.Entitys.ProductEntity>, IEnumerable<Models.Product>>(products);
            return model;

        }

        public void update(Product product)
        {
            product.DateOfChange = DateTime.Now;
            pgm.update(Mapper.Map<Product, DataAcsess.Entitys.ProductEntity>(product));
        }


        public void save(Product product)
        {
            product.DateOfCreate = DateTime.Now;
            product.DateOfChange = DateTime.Now;
            pgm.save(Mapper.Map<Product, DataAcsess.Entitys.ProductEntity>(product));
        }
        public void delete(Guid id)
        {
            pgm.delete(id);
        }



        public Product getByCode(string code)
        {
            var productEntity = (from t in pgm.getAll()
                                 where (t.Code.Equals(code))
                                 select t).SingleOrDefault();
            return Mapper.Map<DataAcsess.Entitys.ProductEntity, Models.Product>(productEntity);
        }



        public IEnumerable<Product> getAllPublished()
        {
            return (from t in getAll()
                    where t.Published.Equals(true)
                    select t).ToList();
        }


        public Product getByArticle(string article)
        {
            return (from t in getAll()
                    where t.Article.Equals(article)
                    select t).First();
        }


        public IEnumerable<Product> searchByArticle(string article)
        {
            var result = (from t in getAllPublished()
                          where t.Article.ToUpper().Replace("-", "")
                                .Contains(article.ToUpper().Replace("-", ""))
                          select t).ToList();

            return result;
        }


        public int getLastIndex()
        {
            var list = (from t in getAll()
                        select t.Index).ToList();
            if (list.Count() != 0)
            {
                return list.Max();
            }
            else return 0;
        }
    }
}
